package com.ymatou.iapi.productmanager.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.tuple.Triple;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.SetNewProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;


public class Ts_SetNewProduct {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static SetNewProductCall setNewProductCall  = new SetNewProductCall();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置新品");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setNewProductCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetNewProduct_001() {
		Logger.start(true, "设置新品,符合条件");
		try{
			String productId1 = ProductFactory.getOnlyProduct(sellerId,sellerName,MapUtil.hashMap("Product->IsNew",false)).getProductId();
			String productId2 = ProductFactory.getOnlyProduct(sellerId,sellerName,MapUtil.hashMap("Product->IsNew",false)).getProductId();
			
			JSONObject ret = setNewProductCall.callNotOverThreshold(Arrays.asList(productId1,productId2));
			Logger.verifyEquals("ok", ret.getString("response"), "返回值为ok");
			
			checkNewProduct(MapUtil.hashMap(productId1,true,productId2,true));
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetNewProduct_002() {
		Logger.start(true, "设置新品,不符合照片条件");
		try{
			String productId1 = ProductFactory.getOnlyProduct(sellerId,sellerName,MapUtil.hashMap("Product->IsNew",false)).getProductId();
			String productId2 = ProductFactory.getOnlyProduct(sellerId,sellerName,MapUtil.hashMap("Product->IsNew",false)).getProductId();
			
			List<Triple<String,Boolean,Boolean>> prodAndOverPicAndOverTxtList = new ArrayList<>();
			prodAndOverPicAndOverTxtList.add(Triple.of(productId1, false, false));
			prodAndOverPicAndOverTxtList.add(Triple.of(productId2, true, false));
			
			JSONObject ret = setNewProductCall.call(prodAndOverPicAndOverTxtList);
			Logger.verifyEquals("ok", ret.getString("response"), "返回值为ok");
			
			checkNewProduct(MapUtil.hashMap(productId1,true,productId2,false));
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetNewProduct_003() {
		Logger.start(true, "设置新品,不符合文字条件");
		try{
			String productId1 = ProductFactory.getOnlyProduct(sellerId,sellerName,MapUtil.hashMap("Product->IsNew",false)).getProductId();
			String productId2 = ProductFactory.getOnlyProduct(sellerId,sellerName,MapUtil.hashMap("Product->IsNew",false)).getProductId();
			
			List<Triple<String,Boolean,Boolean>> prodAndOverPicAndOverTxtList = new ArrayList<>();
			prodAndOverPicAndOverTxtList.add(Triple.of(productId1, false, false));
			prodAndOverPicAndOverTxtList.add(Triple.of(productId2, false, true));
			
			JSONObject ret = setNewProductCall.call(prodAndOverPicAndOverTxtList);
			Logger.verifyEquals("ok", ret.getString("response"), "返回值为ok");
			
			checkNewProduct(MapUtil.hashMap(productId1,true,productId2,false));
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	
	@SuppressWarnings("unchecked")
	public static void checkNewProduct(Map<String,Boolean> productAndIsNewMap){
		productAndIsNewMap.forEach((prod,isnew)->{
			try {
				Map<String,Object> mapInSql = ymtProductsIWapper.selectProductByProductIdforMap(prod).get(0);
				
				Logger.verifyEquals(isnew, Boolean.parseBoolean(mapInSql.get("isNew").toString()), "isNew 检查");
				
				if (isnew){
					AssertService.assertTime(YMTDateUtil.getDate(), (Date)mapInSql.get("dListingTime"), 60, "新品开始时间检查 ");
					AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)mapInSql.get("dNewEndtime"), 60, "新品开始时间检查 ");
				}
				
				if (YmtMongoDriverFactroy.IsNewJavaMongoOpen){
					NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(prod);
				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		
		
	}
	
	
}
